package com.baiyang.server.service.system.impl;


import com.alibaba.fastjson.JSONObject;
import com.baiyang.server.model.base.ItemU;
import com.baiyang.server.model.base.UserIntegral;
import com.baiyang.server.model.read.Balance;
import com.baiyang.server.model.system.Resource;
import com.baiyang.server.model.system.UserRole;
import com.baiyang.server.service.base.ItemUService;
import com.baiyang.server.service.base.UserIntegralService;
import com.baiyang.server.service.read.BalanceService;
import com.baiyang.server.service.system.ResourceService;
import com.baiyang.server.service.system.UserRoleService;
import com.baiyang.server.tools.Data;
import com.baiyang.server.tools.DataTool;
import com.baiyang.server.tools.Letter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baiyang.server.dao.system.UserDao;
import com.baiyang.server.model.system.User;
import com.baiyang.server.redis.RedisUtil;
import com.baiyang.server.service.impl.BaseServiceImpl;
import com.baiyang.server.service.system.UserService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 
 * @author XVX
 * BY: @Primary 注解帮助生命@Autowired优先注入的实列
 */
@Service	
public class UserServiceImpl extends BaseServiceImpl<UserDao, User>implements UserService{
	@Autowired
	private RedisUtil redisUtil = null;
	@Autowired
	private UserDao userDao = null;
	@Autowired
	private UserRoleService userRoleService = null;
	@Autowired
	private BalanceService balanceService = null;
	@Autowired
	private ResourceService resourceService = null;
	@Autowired
	private UserIntegralService userIntegralService = null;
	@Autowired
	private ItemUService itemUService = null;
	/**
	 * 根绝userName获取用户信息
	 */
	@Override
	public User selectOneByUserName(String username) {
		if(username == null) {
			return null; 
		}
		User user = (User) redisUtil.hget("user", username);
		if(user == null) {
			user = new User();
			user.setUserName(username);
			user = userDao.selectOne(user);
			if(user != null) {
				// 获取头像信息
				Resource resourcePic = new Resource();
				resourcePic.setCreateBy("auto");
				resourcePic.setRelationId(user.getId());
				resourcePic.setRelationType("AVATAR");
				resourcePic.setType("PIC");
				resourcePic = resourceService.selectOne(resourcePic);
				if(resourcePic != null){
					user.setAvatarUrl(resourcePic.getPath());
				}
				redisUtil.hset("user", username, user);
			}
		}
		return user;
	}

	/**
	 * 新增读者用户
	 * @param user
	 */
	@Override
	@Transactional
	public void readerRegister(User user) {
		userDao.insert(user);
		UserRole userRole = new UserRole();
		userRole.setId(DataTool.createUUID());
		userRole.setUserId(user.getId());
		userRole.setRoleId(Data.DataDictionary.READER_ROLE_ID);
		userRole.setCreateBy("auto");
		userRoleService.insert(userRole);
		// 创建余额信息
		Balance balance = new Balance();
		balance.setUserId(user.getId());
		balance.setBalance(0d);
		balanceService.insert(balance);
		// 创建积分信息
		UserIntegral userIntegral = new UserIntegral();
		userIntegral.setIntegralNum(0);
		userIntegral.setUserId(user.getId());
		userIntegral.setSignInCondition("0000000");
		userIntegralService.insert(userIntegral);
		// 创建用户相似度表信息
		User u = new User();
		List<User> list = userDao.selectList(u);
		list.forEach(e->{
			ItemU itemU = new ItemU();
			itemU.setId(DataTool.createUUID());
			itemU.setUserId(user.getId());
			itemU.setRelationId(e.getId());
			itemU.setSimilarity(0d);
			itemUService.insert(itemU);
		});
	}

	/**
	 * 新增作者信息
	 * @param user
	 */
	@Override
	public void authorRegister(User user) {
		userDao.insert(user);
		// 创建角色关联信息
		UserRole userRole = new UserRole();
		userRole.setId(DataTool.createUUID());
		userRole.setUserId(user.getId());
		userRole.setRoleId(Data.DataDictionary.AUTHOR_ROLE_ID);
		userRole.setCreateBy("auto");
		userRoleService.insert(userRole);
		// 将头像信息插入数据库
		Resource resourcePic = new Resource();
		resourcePic.setId(DataTool.createUUID());
		resourcePic.setCreateBy("auto");
		resourcePic.setRelationId(user.getId());
		resourcePic.setRelationType("AVATAR");
		resourcePic.setType("PIC");
		resourcePic.setPath(user.getAvatarUrl());
		resourceService.insert(resourcePic);
	}

	/**
	 * 获取书币和积分
	 * @param user
	 * @return
	 */
	@Override
	public JSONObject getBookCoinAndIntegral(User user) {
		JSONObject object = new JSONObject();
		UserIntegral userIntegral = new UserIntegral();
		userIntegral.setUserId(user.getId());
		userIntegral = userIntegralService.selectOne(userIntegral);
		if(userIntegral!=null){
			object.put("integral",userIntegral.getIntegralNum());
		}
		Balance balance = new Balance();
		balance.setUserId(user.getId());
		balance = balanceService.selectOne(balance);
		if(balance != null){
			object.put("bookCoin",balance.getBalance().intValue());
		}
		return object;
	}
}
